package com.ambow.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLEngineResult.Status;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.Entity.Cell;
import com.ambow.service.CellService;
import com.ambow.service.impl.CellServiceImpl;

/**
 * Servlet implementation class CellServlet
 */

public class CellServlet extends HttpServlet {

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String status = request.getParameter("status");
	
		CellService cellService = new CellServiceImpl();
		if (status.equals("add")) {//添加架位
			String cellName = request.getParameter("cellName");
			String remark = request.getParameter("remark");
			if (cellName != "" && cellName != null) {
				Cell cell = new Cell();
				cell.setName(cellName);
				cell.setRemark(remark);
				cellService.saveSingle(cell);
			}

			request.getRequestDispatcher("pages/module/storage/storage006/shelf_list.jsp").forward(request, response);
		} else if (status.equals("query")) {//查询架位
			String shelfName = request.getParameter("shelfName");
			System.out.println("3454657670jaja:"+shelfName);
			if (shelfName=="") {

				List<Cell> list = cellService.queryAll("from Cell", null);
				request.setAttribute("list", list);
			} else {
				String param[]= {shelfName};
				List<Cell> list=new ArrayList<Cell>();
				list.add(cellService.queryOne("from Cell where name=?", param));
				request.setAttribute("list", list);
			}
				request.getRequestDispatcher("pages/module/storage/storage006/shelf_list.jsp").forward(request,
						response);

		} else if (status.equals("modify")) {//修改架位信息
			String cellName = request.getParameter("cellName");
			String remark = request.getParameter("remark");
			String cellid = request.getParameter("cellid");
			Cell cell = new Cell();
			if (cellName != "" && cellName != null) {
				cell.setId(Integer.valueOf(cellid));
				cell.setName(cellName);
				cell.setRemark(remark);
				cellService.update(cell);
				request.getRequestDispatcher("pages/module/storage/storage006/shelf_list.jsp").forward(request,
						response);

			}

		}else if(status.equals("delete")) {
			int cellId =Integer.valueOf(request.getParameter("cellid"));
		boolean f=cellService.delete(cellService.getOne(Cell.class, cellId));
		if(f=true) {
			request.getRequestDispatcher("pages/module/storage/storage006/shelf_list.jsp").forward(request,
					response);
	
		}else {
			  PrintWriter out=response.getWriter();
			out.println("删除失败！！！");
		}
		}
	}
}
